// pages/index/component/watchList/watchList.js
import {
  articleCategoryList,
  articleList
} from '../../../../api/article';
import util from '../../../../utils/util'
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    articleCategoryId: '',
    isIndex: 0,
    title: [],
    pageNum: 1,
    watchList: []
  },

  lifetimes: {
    attached() {
      this.loadCategory()
    }
  },
  pageLifetimes: {
    ready() {

      this.triggerEvent('getCanHeight')
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onReachBottom() {
      this.data.pageNum += 1
      this.loadList()
    },
    downRefresh() {
      this.data.pageNum = 1
      this.data.watchList = []
      this.loadCategory()
    },
    loadList() {
      articleList({
        pageNum: this.data.pageNum,
        articleType: 1,
        // sortField:'orders',
        sortField:'createDate',
        direction:'desc',
        articleCategoryId: this.data.articleCategoryId
      }).then(res => {
        res.data.data.forEach(item => {
          item.thumbnail = util.thumbnail(item.thumbnail, 211, 203)
          item.createDate = this.timeago(item.createDate)
          this.data.watchList.push(item)
          console.log("9999999999");
          console.log(this.data.watchList);
        })
        this.setData({
          watchList: this.data.watchList
        })
        this.triggerEvent('getHotHeight')
      })
    },
    loadCategory() {
      articleCategoryList({
        articleType: 1
      }).then(res => {
        this.data.title = res.data
        if (res.data.length > 0) {
          this.data.articleCategoryId = res.data[0].id
        }

        this.loadList()
        this.setData({
          title: this.data.title
        })
      })
    },
    timeago(timestamp) {
      var d_minutes, d_hours, d_days, d_seconds;
      var timeNow = parseInt(new Date().getTime() / 1000);
      d_seconds = timeNow - timestamp / 1000;
      d_days = parseInt(d_seconds / 86400);
      d_hours = parseInt(d_seconds / 3600);
      d_minutes = parseInt(d_seconds / 60);
      if (d_days > 0 && d_days < 4) {
        return d_days + "天前";
      } else if (d_days <= 0 && d_hours > 0) {
        return d_hours + "小时前";
      } else if (d_hours <= 0 && d_minutes > 0) {
        return d_minutes + "分钟前";
      } else if (d_minutes <= 0 && d_seconds > 0) {
        return d_seconds + "秒前";
      } else {
        return util.datetime(timestamp);
      }
    },
    toDetail(e) {
      let dataset = e.currentTarget.dataset
      if (dataset.linkType === 1) {
        wx.navigateTo({
          url: '/pages/member/webview/index?url=' + dataset.linkAddress,
        })
      } else {
        wx.navigateTo({
          url: '/pages/hotspot/hotspot/index?id=' + e.currentTarget.dataset.id,
        })
      }
    },
    toTitle(data) {
      this.data.isIndex = data.currentTarget.dataset.index
      this.data.articleCategoryId = data.currentTarget.dataset.id
      this.data.pageNum = 1
      this.data.watchList = []
      this.loadList()
      this.setData({
        isIndex: this.data.isIndex,
        scrollLeft: (data.currentTarget.dataset.index - 1) * 228
      })
    },
    scroll(data) {
      console.log(data)
    },
  }
})